package com.richapp.launcher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.widget.Toast;

import com.jack.netty.log.LoggerManager;
import com.richapp.launcher.utils.Tools;

/**
 * Created by jack on 16/2/25.
 */
public class PackageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {


        if(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
            LoggerManager.getInstance().getiLogger().info(getClass(),"收到包安装动作，package:"+intent.getDataString());
            if(context.getPackageName().equals(intent.getDataString())){

                LoggerManager.getInstance().getiLogger().info(getClass(), "更新成功");
                Toast toast = Toast.makeText(context, "更新成功", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();

                Toast toast2 = Toast.makeText(context, "系统将重启更新", Toast.LENGTH_LONG);
                toast2.setGravity(Gravity.CENTER, 0, 0);
                toast2.show();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LoggerManager.getInstance().getiLogger().info(getClass(), "系统将重启更新");
                        Tools.restartSystem(context, "系统将重启更新");
                    }
                }, 5000);
            }

        }else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)){
            LoggerManager.getInstance().getiLogger().info(getClass(),"收到包删除动作，package:"+intent.getDataString());
        }
    }
}
